﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;
using Frame.Lib.Data;

namespace Jzy.DAL.Project
{
    public class TaskScheduleDAL:BaseDAL
    {
        private string _taskID;
        public TaskScheduleDAL(string taskID)
        {
            this._taskID = taskID;
        }
        public static DataTable GetLastRemark(List<int> prjIDs)
        {
            string ids = ConvertUtil.ConvertToDbStr(prjIDs);

            string sql = "select a.*, b.PrjID from " + OTableName.PrjTaskSchedule + " a inner join " + OTableName.PrjTask + " b on a.TaskID = b.ID where a.ID in " +
                        "(select max(c.id) from " + OTableName.PrjTaskSchedule + " c inner join " + OTableName.PrjTask + " d on c.TaskID = d.ID inner join Project e on d.PrjID = e.ID " +
                        "where e.ID in (" + ids + ") Group by e.ID)";


            DataTable dt = DBHelper.Instance().Query(sql);
            return dt;
        }

        public string GetLastID()
        {
            string sql = "select max(ID) from " + OTableName.PrjTaskSchedule + " where TaskID = " + _taskID;
            object id = dbOper.GetScalar<object>(sql);
            if (id is DBNull)
            {
                return null;
            }
            else
            {
                return id.ToString();
            }
        }

        public DataTable GetDataTable()
        {
            string sql = "select *,(select Name from " + OTableName.PrjTask + " where ID = TaskID) as TaskName from " + OTableName.PrjTaskSchedule + " where TaskID = @p0 order by InputTime desc";
            DataTable dt = dbOper.Query(sql,_taskID);
            return dt;
        }

        public static DataTable GetDataTableByPrjID(string prjID)
        {
            string sql = "select *,(select Name from " + OTableName.PrjTask + " where ID = TaskID) as TaskName from " + OTableName.PrjTaskSchedule +
                         " where TaskID in (select ID from " + OTableName.PrjTask + " where PrjID = @p0) order by InputTime desc";
            DataTable dt = DBHelper.Instance().Query(sql, prjID);
            return dt;
        }
    }
}
